{% extends "base.html" %}
{% import "bootstrap/wtf.html" as wtf %}
{% import "_macros.html" as macros %}

{% block title %}语音识别模型基准测试系统{% endblock %}

{% block page_content %}
<div class="page-header">
    {% if is_searched %}
    <p>{{ kw }}搜索结果共有{{ lms.total }}条语言模型数据项</p>
    <a class="btn btn-default" href="{{ url_for('.lm') }}">查看所有语言模型信息</a>
    {% else %}
    <p>目前共有{{ lms.total }}条语言模型数据项</p>
    <a class="btn btn-default" href="{{ url_for('.lm_add') }}">新增语言模型信息</a>
    {% endif %}
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="text" id="kw-input" onkeyup="update_action(event,this.value);" value="{{ kw }}">
    <a class="btn btn-default" id="search-a" href="{{ url_for('.lm_search',kw='') }}">搜索</a><br>
    <p>筛选条件：</p>
    <p>原理：<input type="checkbox" id="ngram-search">n元模型&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="dl-search">深度学习&nbsp;&nbsp;&nbsp;&nbsp;<span id="open-search-span"><a style="color:blue;" href="javascript:void(0);" onclick="open_search();">展开</a></span></p>
    <p id="filetype-search-p" style="display:none;">模型文件类型：<input type="checkbox" id="source-search">源代码文件&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="framework-save-search">深度学习框架导出的模型文件</p>
    <p id="content-search-p" style="display:none;">模型文件内容：<input type="checkbox" id="structure-search">结构&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="structure-weights-search">结构+权重</p>
    <p id="weights-type-search-p" style="display:none;">权重文件类型：<input type="checkbox" id="keras-weights-search">由keras保存的权重文件&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="keras-ckpt-search">由keras保存的检查点文件&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="torch-state-dict-search">由pytorch保存的参数文件</p>
    <p id="file-ext-search-p" style="display:none;">深度学习框架导出的结构+权重模型文件的类型：<input type="checkbox" id="saved-model-search">tf_saved_model&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="json-search">tf_json&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="config-search">tf_config&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="torch-model-search">pytorch_model</p>
    <p id="framework-search-p" style="display:none;">模型所依赖的框架：<input type="checkbox" id="tf-keras-search">tf.keras&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="torch-nn-search">torch.nn<span><a id="close-search-a" style="color:blue;" href="javascript:void(0);" onclick="close_search();">闭合</a></span></p>
    {% if lms.first==0 %}
    <p>没有{% if kw %}含有{{ kw }}的{% endif %}语言模型信息</p>
    {% else %}
    <div class="row">
    <table class="table">
        <thead>
        <tr>
            <th>名称</th>
            <th>类型</th>
            <th>文件类型</th>
            <th>内容</th>
            <th>文件来源</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        {% for lm in lms.items %}
        <tr>
            <td class="content-td">{{ lm.name }}</td>
            <td>{% if lm.typ==0 %}n元语法{% elif lm.typ==1 %}深度神经网络{% endif %}</td>
            <td>
                {% if lm.filetype==0 %}源代码{% elif lm.filetype==1 %}机器学习框架保存的模型文件{% endif %}
            </td>
            <td>{% if lm.content==0 %}结构{% elif lm.content==1 %}结构+权重{% endif %}</td>
            <td class="content-td">{{ lm.file }}</td>
            <td>
                <a class="btn btn-default" href="{{ url_for('.lm_edit',id=lm.id) }}">修改</a>
                <a class="btn btn-default" href="{{ url_for('.lm_delete',id=lm.id,page=page,per_page=per_page) }}">删除</a>
                <a class="btn btn-default" href="{{ url_for('.language_model',id=lm.id)}}">查看语言模型详情</a>
                <a class="btn btn-default" href="{{ url_for('.lm_active',id=lm.id) }}">更新活动时间</a>
                <a class="btn btn-default" href="{{ url_for('.lm_copy',id=lm.id) }}">以此为模版新建</a>
            </td>
        </tr>
        {% endfor %}
        </tbody>
        <tfoot>
            {% if is_searched %}
            <tr>
                <td colspan="3">
                    <ul class="pagination">
                        {% for i in page_range %}
                        {% if i==lms.page %}
                        <li class="active"><a href="/lm_search?page={{ i }}&per_page={{ per_page }}&{{ params }}">{{ i }}</a></li>
                        {% else %}
                        <li><a href="/lm_search?page={{ i }}&per_page={{ per_page }}&{{ params }}">{{ i }}</a></li>
                        {% endif %}
                        {% endfor %}
                    </ul>
                </td>
                <td colspan="3" style="vertical-align:bottom">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">当前每页{{ per_page }}条数据项，更改至每页数据项 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            {% for i in [5,10,20,50] %}
                            <li><a href="/lm_search?page=1&per_page={{ i }}&{{ params }}">{{ i }}条</a></li>
                            {% endfor %}
                        </ul>
                    </li>
                </td>
            </tr>
            {% else %}
            <tr>
                <td colspan="3">
                    <ul class="pagination">
                        {% for i in page_range %}
                        {% if i==lms.page %}
                        <li class="active"><a href="/lm?page={{ i }}&per_page={{ per_page }}">{{ i }}</a></li>
                        {% else %}
                        <li><a href="/lm?page={{ i }}&per_page={{ per_page }}">{{ i }}</a></li>
                        {% endif %}
                        {% endfor %}
                    </ul>
                </td>
                <td colspan="3" style="vertical-align:bottom">
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">当前每页{{ per_page }}条数据项，更改至每页数据项 <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            {% for i in [5,10,20,50] %}
                            <li><a href="/lm?page=1&per_page={{ i }}">{{ i }}条</a></li>
                            {% endfor %}
                        </ul>
                    </li>
                </td>
            </tr>
            {% endif %}
        </tfoot>
    </table>
</div>
    {% endif %}
    <p id="kw-value" style="display:none">{{ kw }}</p>
<script type="text/javascript">
    function update_action(event,s){
        if(s.length!=0){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace(new RegExp('\\?kw\\=[^&]*','gi'),'?kw='+s));
            if(event.key=='Enter'){
                window.location.href=$('#search-a').attr('href');
            }
        }
    }

    function open_search(){
        $('#filetype-search-p').attr('style','display:block');
        $('#content-search-p').attr('style','display:block');
        $('#weights-type-search-p').attr('style','display:block');
        $('#file-ext-search-p').attr('style','display:block');
        $('#framework-search-p').attr('style','display:block');
        $('#open-search-span').attr('style','display:none');
    }
    
    function close_search(){
        $('#filetype-search-p').attr('style','display:none');
        $('#content-search-p').attr('style','display:none');
        $('#weights-type-search-p').attr('style','display:none');
        $('#file-ext-search-p').attr('style','display:none');
        $('#framework-search-p').attr('style','display:none');
        $('#open-search-span').attr('style','display:inline;')
    }

    $('#ngram-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&ngram=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&ngram=1',''));
        }
    })

    $('#dl-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&dl=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&dl=1',''));
        }
    })

    $('#source-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&source=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&source=1',''));
        }
    })

    $('#framework-save-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&framework_save=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&framework_save=1',''));
        }
    })

    $('#structure-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&structure=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&structure=1',''));
        }
    })

    $('#structure-weights-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&structure_weights=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&structure_weights=1',''));
        }
    })

    $('#keras-weights-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&keras_weights=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&keras_weights=1',''));
        }
    })

    $('#keras-ckpt-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&keras_ckpt=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&keras_ckpt=1',''));
        }
    })

    $('#torch-state-dict-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&torch_state_dict=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&torch_state_dict=1',''));
        }
    })

    $('#saved-model-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&saved_model=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&saved_model=1',''));
        }
    })

    $('#json-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&json=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&json=1',''));
        }
    })

    $('#config-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&config=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&config=1',''));
        }
    })

    $('#torch-model-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&torch_model=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&torch_model=1',''));
        }
    })

    $('#tf-keras-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&tf_keras=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&tf_keras=1',''));
        }
    })

    $('#torch-nn-search').change(function(){
        if(this.checked==true){
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href+'&torch_nn=1');
        }else{
            old_href=$('#search-a').attr('href');
            $('#search-a').attr('href',old_href.replace('&torch_nn=1',''));
        }
    })
</script>
{% if is_searched and kw %}
<script type="text/javascript">
    $(document).ready(function(){
        var query=$('#kw-value').text();
        var content_tds=document.getElementsByClassName('content-td');
        var regex=new RegExp(query,'gi');
        for(let i=0;i<content_tds.length;i++){
            content_tds[i].innerHTML=content_tds[i].innerHTML.replace(regex,'<font style="color:red;">$&</font>');
        }
    });
</script>
{% endif %}
{% if is_searched %}
<script type="text/javascript">
    $(document).ready(function(){
    var url=window.location.href;
    // 设置search-a.href为url
    $('#search-a').attr('href',url.replace(new RegExp('&page=[^&]*','gi'),'').replace(new RegExp('&per_page=[^&]*','gi'),''));
    // 选中要搜索的多选框
    var checkbox_kws=url.substring(url.indexOf('&')).split('=1');
    for(let i=0;i<checkbox_kws.length-1;i++){
        switch(checkbox_kws[i].substring(1)){
            case 'ngram':
                $('#ngram-search').attr('checked','checked');
                break;
            case 'dl':
                $('#dl-search').attr('checked','checked');
                break;
            case 'source':
                $('#source-search').attr('checked','checked');
                break;
            case 'framework_save':
                $('#framework-save-search').attr('checked','checked');
                break;
            case 'structure':
                $('#structure-search').attr('checked','checked');
                break;
            case 'structure_weights':
                $('#structure-weights-search').attr('checked','checked');
                break;
            case 'keras_weights':
                $('#keras-weights-search').attr('checked','checked');
                break;
            case 'keras_ckpt':
                $('#keras-ckpt-search').attr('checked','checked');
                break;
            case 'torch_state_dict':
                $('#torch-state-dict-search').attr('checked','checked');
                break;
            case 'saved_model':
                $('#saved-model-search').attr('checked','checked');
                break;
            case 'json':
                $('#json-search').attr('checked','checked');
                break;
            case 'config':
                $('#config-search').attr('checked','checked');
                break;
            case 'torch_model':
                $('#torch-model-search').attr('checked','checked');
                break;
            case 'tf_keras':
                $('#tf-keras-search').attr('checked','checked');
                break;
            case 'torch_nn':
                $('#torch-nn-search').attr('checked','checked');
                break;
            default:
                ;
        }
    }
})
</script>
{% endif %}
</div>

{% endblock %}